1include <BOSL/math.scad>
 2include <BOSL/convex_hull.scad>
 3
 4
 5testpoints_on_sphere = [ for(p = 
 6	[
 7		[1,PHI,0], [-1,PHI,0], [1,-PHI,0], [-1,-PHI,0],
 8		[0,1,PHI], [0,-1,PHI], [0,1,-PHI], [0,-1,-PHI],
 9		[PHI,0,1], [-PHI,0,1], [PHI,0,-1], [-PHI,0,-1]
10	])
11	normalize(p)
12];
13
14testpoints_circular = [ for(a = [0:15:360-EPSILON]) [cos(a),sin(a)] ];
15
16testpoints_coplanar = let(u = normalize([1,3,7]), v = normalize([-2,1,-2])) [ for(i = [1:10]) rands(-1,1,1)[0] * u + rands(-1,1,1)[0] * v ];
17
18testpoints_collinear_2d = let(u = normalize([5,3]))    [ for(i = [1:20]) rands(-1,1,1)[0] * u ];
19testpoints_collinear_3d = let(u = normalize([5,3,-5])) [ for(i = [1:20]) rands(-1,1,1)[0] * u ];
20
21testpoints2d = 20 * [for (i = [1:10]) concat(rands(-1,1,2))];
22testpoints3d = 20 * [for (i = [1:50]) concat(rands(-1,1,3))];
23
24// All points are on the sphere, no point should be red
25translate([-50,0]) visualize_hull(20*testpoints_on_sphere);
26
27// 2D points
28translate([50,0]) visualize_hull(testpoints2d);
29
30// All points on a circle, no point should be red
31translate([0,50]) visualize_hull(20*testpoints_circular);
32
33// All points 3d but collinear
34translate([0,-50]) visualize_hull(20*testpoints_coplanar);
35
36// Collinear
37translate([50,50]) visualize_hull(20*testpoints_collinear_2d);
38
39// Collinear
40translate([-50,50]) visualize_hull(20*testpoints_collinear_3d);
41
42// 3D points
43visualize_hull(testpoints3d);
44
45
46module visualize_hull(points) {
47	hull = convex_hull(points);
48	
49	%if (len(hull) > 0 && is_list(hull[0]) && len(hull[0]) > 0)
50		polyhedron(points=points, faces = hull);
51	else
52		polyhedron(points=points, faces = [hull]);
53	
54	for (i = [0:len(points)-1]) {
55		p = points[i];
56		$fn = 16;
57		translate(p) {
58			if (hull_contains_index(hull,i)) {
59				color("blue") sphere(1);
60			} else {
61				color("red") sphere(1);
62			}
63		}
64	}
65	
66	function hull_contains_index(hull, index) = 
67		search(index,hull,1,0) ||
68		search(index,hull,1,1) ||
69		search(index,hull,1,2);
70}
71
72
73// vim: noexpandtab tabstop=4 shiftwidth=4 softtabstop=4 nowrap